package login;

import java.util.ArrayList;

import dbTable.ACCOUNT;
public class SignBean {
	
	private String name;
	private String tel;
	private String email;
	private String pw;	
	private String rePw;
	private String nickName;
	private String sex;
	private String check;
	private String exe;
	private int result;
	private ArrayList<String> err = new ArrayList<String>();	
	public static final int OK = 1;
	public static final int FAIL = 2;
	private String[] error = new String[7];
		
	
	public String[] getError() {
		return error;
	}

	public void setError(String[] error) {
		this.error = error;
	}

	public String getExe() {
		ACCOUNT a = new ACCOUNT();
		long adddate = System.currentTimeMillis();
		boolean s = false;
		if(sex.equals("0")){
			s=false;
		}else{
			s=true;
		}
		a.insert(name, pw, tel, nickName, s, email, adddate);
		return exe;
	}
	
	public void setExe(String exe) {
		this.exe = exe;
	}
	
	public String getCheck() {
		getName();		//0
		getTel();		//1
		getNickName();	//2
		getSex();		//3
		getEmail();		//4
		getPw();		//5
		getRePw();		//6
		if(result!=FAIL){
			result = SignBean.OK;
		}
		return "";
	}
	public void setCheck(String check) {
		this.check = check;
	}
	public String getName() {
		if (name == null || name.equals("")) {
			error[0]="姓名欄位空白";
			//err.add("姓名欄位空白");
			result = SignBean.FAIL;
			return "";
		} else if (name.indexOf(' ') != -1) {
			error[0]="姓名欄位不允許空白字元";
			//err.add("姓名欄位不允許空白字元");
			result = SignBean.FAIL;
			return "";
		}
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		if (tel == null || tel.equals("")) {
			error[1]="電話欄位空白";
			//err.add("電話欄位空白");
			result = SignBean.FAIL;
			return "";
		} else if (tel.indexOf(' ') != -1) {
			error[1]="電話欄位不允許空白字元";
			//err.add("電話欄位不允許空白字元");
			result = SignBean.FAIL;
			return "";
		}		
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	public String getEmail() {
		if (email == null || email.equals("")) {
			error[4]="信箱欄位空白";
			//err.add("信箱欄位空白");
			result = SignBean.FAIL;
			return "";
		} else {
			String regemail = "^[_a-z0-9-]+(.[_a-z0-9-]+)*"
					+ "@[a-z0-9-]+([.][a-z0-9-]+)*$";
			String mail = email.toLowerCase();
			if (!mail.matches(regemail)) {
				error[4]="請輸入正確信箱格式";
				//err.add("請輸入正確信箱格式");
				result = SignBean.FAIL;
				return "";
			}
		}
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getRePw() {
		if (pw != null && !pw.equals("")) {
			if (rePw != null && ! pw.equals("")) {
				if (!rePw.equals(pw)) {
					error[6]="密碼上下欄位不相符";
					//err.add("密碼上下欄位不相符");
					result = SignBean.FAIL;
					return "";
				}
			}else{
				error[6]="確認密碼欄位空白";
				//err.add("確認密碼欄位空白");
				result = SignBean.FAIL;
				return "";
			}
		}
		return rePw;
	}
	public void setRePw(String rePw) {
		this.rePw = rePw;
	}
	public String getPw() {
		if (pw == null || pw.equals("")) {
			error[5]="密碼欄位空白";
			//err.add("密碼欄位空白");
			result = SignBean.FAIL;
			return "";
		} else if (pw.length() > 7 || pw.length() < 3) {
			error[5]="密碼不足3個字元或超過6個字元";
			//err.add("密碼不足3個字元或超過6個字元");
			result = SignBean.FAIL;
			return "";
		} else if (pw.indexOf(' ') != -1) {
			error[5]="密碼不允許空白字元";
			//err.add("密碼不允許空白字元");
			result = SignBean.FAIL;
			return "";
		}
		return pw;
	}
	public void setPw(String pw) {
		this.pw = pw;
	}
	public String getNickName() {
		if (nickName == null || nickName.equals("")) {
			error[2]="暱稱欄位空白";
			//err.add("暱稱欄位空白");
			result = SignBean.FAIL;
			return "";
		} else if (nickName.indexOf(' ') != -1) {
			error[2]="暱稱欄位不允許空白字元";
			//err.add("暱稱欄位不允許空白字元");
			result = SignBean.FAIL;
			return "";
		}	
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getSex() {
		if(sex==null || sex.equals("")){
			error[3]="性別沒勾選";
			//err.add("性別沒勾選");
			result = SignBean.FAIL;
			return "";
		}
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getResult() {
		return result;
	}
	public void setResult(int result) {
		this.result = result;
	}
	public ArrayList<String> getErr() {
		return err;
	}
	public void setErr(ArrayList<String> err) {
		this.err = err;
	}
	public static int getOk() {
		return OK;
	}
	public static int getFail() {
		return FAIL;
	}
	
}
